package com.lineage.server.command.executor;

import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.L1SpawnUtil;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1SpawnCmd implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1SpawnCmd.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1SpawnCmd();
  }
  
  private void sendErrorMessage(L1PcInstance pc, String cmdName) {
    String errorMsg = String.valueOf(String.valueOf(cmdName)) + " npcid|name [数量] [范围]。";
    pc.sendPackets((ServerBasePacket)new S_SystemMessage(errorMsg));
  }
  
  private int parseNpcId(String nameId) {
    int npcid = 0;
    try {
      npcid = Integer.parseInt(nameId);
    } catch (NumberFormatException e) {
      npcid = NpcTable.get().findNpcIdByNameWithoutSpace(nameId);
    } 
    return npcid;
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      String nameId = tok.nextToken();
      int count = 1;
      if (tok.hasMoreTokens())
        count = Integer.parseInt(tok.nextToken()); 
      int randomrange = 0;
      if (tok.hasMoreTokens())
        randomrange = Integer.parseInt(tok.nextToken(), 10); 
      int npcid = parseNpcId(nameId);
      L1Npc npc = NpcTable.get().getTemplate(npcid);
      if (npc == null) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("找不到该npc。"));
        return;
      } 
      if (count > 1000) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("一次召唤数量不能超过1000。"));
        return;
      } 
      if (count > 100) {
        SpawnRunnable spawnRunnable = new SpawnRunnable(pc, npcid, randomrange, count);
        GeneralThreadPool.get().execute(spawnRunnable);
      } else {
        int i = 0;
        while (i < count) {
          L1SpawnUtil.spawn(pc, npcid, randomrange, 0);
          i++;
        } 
      } 
      String msg = String.format("%s(%d) (%d) 召唤。 (范围:%d)", new Object[] { npc.get_name(), Integer.valueOf(npcid), 
            Integer.valueOf(count), Integer.valueOf(randomrange) });
      pc.sendPackets((ServerBasePacket)new S_SystemMessage(msg));
    } catch (NoSuchElementException e) {
      sendErrorMessage(pc, cmdName);
    } catch (NumberFormatException e2) {
      sendErrorMessage(pc, cmdName);
    } catch (Exception e3) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
  
  private class SpawnRunnable implements Runnable {
    private L1PcInstance _pc;
    
    private int _npcid;
    
    private int _randomrange;
    
    private int _count;
    
    private SpawnRunnable(L1PcInstance pc, int npcid, int randomrange, int count) {
      this._pc = pc;
      this._npcid = npcid;
      this._randomrange = randomrange;
      this._count = count;
    }
    
    public void run() {
      try {
        int i = 0;
        while (i < this._count) {
          L1SpawnUtil.spawn(this._pc, this._npcid, this._randomrange, 0);
          Thread.sleep(1L);
          i++;
        } 
      } catch (InterruptedException e) {
        L1SpawnCmd._log.error(e.getLocalizedMessage(), e);
      } 
    }
  }
}
